<template>
    <el-dialog
            :title="!dataForm.orderId ? '新增' : '审核'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                <el-form-item label="货物描述" prop="goodsDescription" >
                        <el-input v-model="dataForm.goodsDescription" placeholder="货物描述" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="货物重量（单位：千克）" prop="weight" >
                        <el-input v-model="dataForm.weight" placeholder="货物重量（单位：千克）" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="货物体积（单位：立方米）" prop="volume" >
                        <el-input v-model="dataForm.volume" placeholder="货物体积（单位：立方米）" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="起运地" prop="origin" >
                        <el-input v-model="dataForm.origin" placeholder="起运地" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="目的地" prop="destination" >
                        <el-input v-model="dataForm.destination" placeholder="目的地" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="预计运输时间" prop="expectedDeliveryTime" >
                        <el-date-picker
                                v-model="dataForm.expectedDeliveryTime"
                                type="datetime"
                                placeholder="预计运输时间" :disabled="true">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="订单状态" prop="orderStatus">
                    <el-select
                        v-model="dataForm.orderStatus"
                        placeholder="请选择订单状态"
                        style="width: 100%;"
                    >
                        <el-option
                        label="未审核"
                        value="未审核"
                        ></el-option>
                        <el-option
                        label="已审核"
                        value="已审核"
                        ></el-option>
                        <el-option
                        label="运输中"
                        value="运输中"
                        ></el-option>
                        <el-option
                        label="已完成"
                        value="已完成"
                        ></el-option>
                    </el-select>
                    </el-form-item>

                <el-form-item label="订单创建时间" prop="createdTime" >
                        <el-date-picker
                                v-model="dataForm.createdTime"
                                type="datetime"
                                placeholder="订单创建时间" :disabled="true">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="订单审核时间" prop="auditTime">
                        <el-date-picker
                                v-model="dataForm.auditTime"
                                type="datetime"
                                placeholder="订单审核时间">
                        </el-date-picker>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            orderId: null,
            shipperId: null,
            consigneeId: null,
            goodsDescription: null,
            weight: null,
            volume: null,
            origin: null,
            destination: null,
            expectedDeliveryTime: null,
            orderStatus: '未审核',
            createdTime: null,
            auditTime: null
          },
          dataRule: {
            orderId: [
                        { required: true, message: '订单ID，主键不能为空', trigger: 'blur' }
            ],
            shipperId: [
                        { required: true, message: '发货人ID，外键关联发货人表不能为空', trigger: 'blur' }
            ],
            consigneeId: [
                        { required: true, message: '收货人ID，外键关联收货人表不能为空', trigger: 'blur' }
            ],
            goodsDescription: [
                        { required: true, message: '货物描述不能为空', trigger: 'blur' }
            ],
            weight: [
                        { required: true, message: '货物重量（单位：千克）不能为空', trigger: 'blur' }
            ],
            volume: [
                        { required: true, message: '货物体积（单位：立方米）不能为空', trigger: 'blur' }
            ],
            origin: [
                        { required: true, message: '起运地不能为空', trigger: 'blur' }
            ],
            destination: [
                        { required: true, message: '目的地不能为空', trigger: 'blur' }
            ],
            expectedDeliveryTime: [
                        { required: true, message: '预计运输时间不能为空', trigger: 'blur' }
            ],
            orderStatus: [
                        { required: true, message: '订单状态（如：未审核、已审核、运输中、已完成）不能为空', trigger: 'blur' }
            ],
            createdTime: [
                        { required: true, message: '订单创建时间不能为空', trigger: 'blur' }
            ],
            auditTime: [
                        { required: true, message: '订单审核时间不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.orderId = id || 0
          console.log('this.dataForm.orderId:' + this.dataForm.orderId)
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.orderId = id || 0
            console.log('this.dataForm.orderId1:' + this.dataForm.orderId)
            if (this.dataForm.orderId) {
              this.$http({
                url: this.$http.adornUrl(`/app/orders/info/${this.dataForm.orderId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.apporders
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/orders/${!this.dataForm.orderId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>